//
//  ComposePicSelectorView.swift
//  WBSwift
//
//  Created by 贺瑞 on 2017/8/11.
//  Copyright © 2017年 herui. All rights reserved.
//

import UIKit

class ComposePicSelectorView : UICollectionView {

    var presentedVC : UIViewController?
    
    
    var images : [UIImage] = [UIImage]()
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        dataSource = self
        delegate = self
        register(ComposePicSelectorViewItem.nib, forCellWithReuseIdentifier: ComposePicSelectorViewItem.reuseID)
        reloadData()
    }

    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let margin : CGFloat = 5
        let itemWH : CGFloat = (bounds.size.width - (margin * 2)) / 3.0
        let layout = collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = CGSize(width: itemWH, height: itemWH)
        layout.minimumLineSpacing = margin
        layout.minimumInteritemSpacing = margin
    }
    
}

extension ComposePicSelectorView : UICollectionViewDataSource, UICollectionViewDelegate {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return min(images.count + 1, 9)
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ComposePicSelectorViewItem.reuseID, for: indexPath) as! ComposePicSelectorViewItem
        cell.delegate = self
        if(indexPath.row < images.count){
            cell.image = images[indexPath.row]
        }else{
            cell.image = nil
        }
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        if indexPath.row < images.count {
            return
        }
        
        
        guard UIImagePickerController.isSourceTypeAvailable(.photoLibrary) else {
            HHLog("相册无法访问")
            return
        }
        
        let picker = UIImagePickerController()
        picker.sourceType = .photoLibrary
        picker.delegate = self
        presentedVC?.present(picker, animated: true, completion: nil)
        
        
        
    }
    
    
    
    
}

extension ComposePicSelectorView :  UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        picker.dismiss(animated: true, completion: nil)
    }
    
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        HHLog(info)
        
        let image = info[UIImagePickerControllerOriginalImage] as! UIImage
//        let referenceURL = info[UIImagePickerControllerReferenceURL]

        images.append(image)
        reloadData()
        
        picker.dismiss(animated: true, completion: nil)
    }
    
}

extension ComposePicSelectorView : ComposePicSelectorViewItemDelegate {
    func composePicSelectorViewItemDelBtnOnclick(cell : ComposePicSelectorViewItem) {
        guard let indexPath = indexPath(for: cell) else {
            return
        }
        images.remove(at: indexPath.row)
        deleteItems(at: [indexPath])
    }
}
